package online.shenme.day11;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class FileTest {
    public static void main(String[] args) throws IOException {
        HashMap<String, Integer> map = new HashMap<>();
        File file = new File("javaadvance\\src\\online\\shenme\\day11\\test");
        createDir(file);
        getCount(map, file);
        deleteDir(file);

    }

    private static void getCount(HashMap<String, Integer> map, File file) {
        for (File listFile : file.listFiles()) {
            if (listFile.isFile()) {
                String fileEnd = listFile.getName().substring(listFile.getName().lastIndexOf(".") + 1);
                if (map.containsKey(fileEnd)) {
                    map.put(fileEnd, map.get(fileEnd) + 1);
                } else {
                    map.put(fileEnd, 1);
                }
            } else {
                getCount(map, listFile);
            }
        }
        map.forEach((end,i)-> System.out.println(end+"--"+i));
    }

    private static void createDir(File file) throws IOException {
        if (!file.exists()) {
            file.mkdirs();
        }
        new File(file, "test.java").createNewFile();
        new File(file, "test1").mkdir();
        new File(file, "test1\\test2.mp4").createNewFile();
        new File(file, "test1\\test3\\test4").mkdirs();
        new File(file, "test3.txt").createNewFile();
    }

    private static void deleteDir(File file) {
        for (File listFile : file.listFiles()) {
            if (listFile.isFile()) {
                listFile.delete();
            } else {
                deleteDir(listFile);
                listFile.delete();
            }
        }
        file.delete();
    }
}
